<!--
 * @Author: caozaihui 15908754730@163.com
 * @Date: 2024-06-14 00:47:40
 * @LastEditors: czh 2119294696@qq.com
 * @LastEditTime: 2024-07-01 23:14:35
 * @FilePath: \YMU_forum_vue\src\components\article\ArticleEditor.vue
    编辑文章 
-->
<script setup>
import { useRoute } from 'vue-router'
import LayoutFooter from '@/components/LayoutFooter.vue';
import { onMounted, ref } from 'vue';
import { addArticle, modifyArticle, getArticleDetails, getTypes } from '@/apis/article.js'
import { ElMessage, ElInput } from 'element-plus'
import { Hide, View } from '@element-plus/icons-vue'
import { uploadImg } from '@/apis/upload.js'
import { getUserTagList } from '@/apis/user'
// 判断新增还是修改
const modify = ref(0)
const msg = ref('')

const route = useRoute()
const article = ref({
    title: '',
    content: '',
    description: '',
    coverImg: '',
    tags: [''],
    // 开启文章评论
    commentabled: true,
    published: true,
    type: ''
})
const articleSubmit = ref({
    aid: 0,
    title: '',
    content: '',
    description: '',
    coverImg: '',
    tagIds: [],
    commentabled: true,
    published: true,
    typeId: ''
})
const fileList = ref([])
// const test = ()=>{
//     console.log("测试"+article.value.tags)
// }
// 文章分类,标签
const types = ref([''])
const tags = ref([''])
onMounted(() => {
    modify.value = route.query.modify
    if (modify.value == 1) {
        getArticleDetails(route.query.aid).then(res => {
            article.value = res.data
            fileList.value.push({
                name: '缩略图',
                url: res.data.coverImg
            })
        });
    } else {
        article.value = {}
    }
    // 获取全部分类信息
    getTypes().then(res => {
        console.log('全部分类', res);
        types.value = res.data
    })
    //获取用户的全部标签
    getUserTagList().then(res => {
        console.log('用户全部标签', res);
        tags.value = res.data.pageInfo.list
    })

})
//上传图片
const handleUpload = (img) => {
    uploadImg(img.file).then(res => {
        if (res.code == 0) {
            article.value.coverImg = res.data
            fileList.value.pop()
            fileList.value.push({ name: img.file.name, url: res.data })
        }
    })
}
const fileRemove = () => {
    fileList.value.pop()
}
const onExceed = () => {
    ElMessage({
        message: '只能上传一张图片',
        type: 'warning',
        plain: true,
    })
}
//报错文章
const save = () => {
    articleSubmit.value.aid = article.value.aid
    articleSubmit.value.coverImg = article.value.coverImg
    articleSubmit.value.content = article.value.content
    articleSubmit.value.description = article.value.description
    articleSubmit.value.title = article.value.title
    articleSubmit.value.commentabled = article.value.commentabled
    articleSubmit.value.published = article.value.published
    if (modify.value == 1) {
        modifyArticle(articleSubmit.value).then(res => {
            console.log('修改文章', res);
            msg.value = ''
            msg.value = res.msg
        });
    } else {
        addArticle(articleSubmit.value).then(res => {
            msg.value = ''
            msg.value = res.msg
        })
    }
}
</script>

<template>
    <el-row style="margin-top: 50px">
        <el-col :lg="8"></el-col>
        <el-col :lg="8" :xs="24">
            <div style="margin-top: 20px;">
                <el-text class="title">文章标题</el-text>
                <el-input class="input" v-model="article.title" placeholder="在这里输入标题" />

                <el-text class="title">文章描述</el-text>
                <el-input class="input" v-model="article.description" placeholder="在这里输入文字" />

                <el-upload v-model:file-list="fileList" action="upload" :http-request="handleUpload" class="upload-demo"
                    :on-remove="fileRemove" list-type="picture" limit='1' name="img" :on-exceed="onExceed" drag>
                    <el-button type="primary">上传封面</el-button>
                    <div class="el-upload__tip">
                        上传jpg/png不超过500kb
                    </div>
                </el-upload>

                <!-- <button @click="test">测试</button> -->
                <div style="display: flex;justify-content: space-between;margin-top: 20px">
                    <el-select v-model="articleSubmit.typeId" :placeholder="article.type?article.type:'选择类型'" size="large" style="width: 200px">
                        <el-option v-for="(item, index) in types" :key="index" :label="item.name"
                            :value="item.typeId" />
                    </el-select>

                    <el-select v-model="articleSubmit.tagIds" multiple collapse-tags placeholder="选择标签(可多选)"
                        style="width: 240px;height: 40px">
                        <el-option v-for="(item, index) in tags" :key="index" :label="item.name"
                            :value="item.tagId" />
                    </el-select>
                </div>
                <div style="display: flex;justify-content: space-between;margin-top: 20px">
                    <el-switch v-model="article.commentabled" class="mb-2"
                        style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text="可评论"
                        inactive-text="不可评论" />
                    <el-switch v-model="article.published" :active-action-icon="View" :inactive-action-icon="Hide" />
                </div>
            </div>
        </el-col>
        <el-col :lg="8"></el-col>
    </el-row>

    <el-row style="margin-top: 50px">
        <el-col :lg="2"></el-col>
        <el-col :lg="22" :xs="24">
            <el-text type="danger" style="margin-left: 15px;margin-top: 10px;">{{ msg }}</el-text>
            <div style="width: 1200px;margin-top: 10px;">
                <v-md-editor v-model="article.content" @save="save" height="100vh"
                    placeholder="使用md格式即可轻松编辑文本 :-) "></v-md-editor>
            </div>
        </el-col>
        <el-col :lg="2"></el-col>
    </el-row>
    <LayoutFooter />
</template>

<style scoped>
.tag {
    margin-right: 5px;
}

.title {
    font-size: 14px;
    font-weight: bolder;
}

.input {
    margin-top: 10px;
    margin-bottom: 20px;
}
</style>